const express = require('express')
const { Sequelize } = require('sequelize');
const category = require('./category')
const application = require('./application')

var isInited = false
var sequelize = undefined


// let sequelize = new Sequelize({
//     dialect: 'sqlite',
//     storage: './resource/database.db'
// })

function init() {
    isInited = true

    sequelize = new Sequelize({
            dialect: 'sqlite',
            storage: './resource/database.db'
        })

    category.init(sequelize)
    application.init(sequelize)

    //sync database.
    sequelize.sync()
}


function uninit() {
    isInited = false

    sequelize.close
}

let route = express.Router()

route.use(category.api())

route.use(application.api())

module.exports = {
    router: function() {
        if(!isInited) {
            init()
        }
        return route
    },
    release: function() {
        if(isInited) {
            uninit()
        }
    }
}